package com.medicine.order.controller;

import com.alibaba.fastjson.JSONObject;
import com.medicine.common.base.ResponseData;
import com.medicine.common.param.OrderParam;
import com.medicine.common.config.RabbitMQConfig;
import com.medicine.order.feign.ProductFginService;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.List;

@RestController
@RequestMapping("/order/order")
public class OrderController {

    @Autowired
    private ProductFginService productFginService;
    @Autowired
    private RabbitTemplate rabbitTemplate;

    @GetMapping("/list")
    public String list(){
        String s = productFginService.list();
        return s;
    }

    @GetMapping("/test")
    public String test(){

        List<String> list = new ArrayList<>();
        return null;
    }

    /**
     * 测试
     */
    @GetMapping("/sendmsg")
    public String sendMsg(@RequestParam String msg, @RequestParam String key){
        /**
         * 发送消息
         * 参数一：交换机名称
         * 参数二：路由key: item.springboot-rabbitmq,符合路由item.#规则即可
         * 参数三：发送的消息
         */
        rabbitTemplate.convertAndSend(RabbitMQConfig.ITEM_TOPIC_EXCHANGE ,key ,msg);
        //返回消息
        return "发送消息成功！";
    }


    @PostMapping("/sendmsgs")
    public ResponseData sendmsg(@RequestBody OrderParam orderParam){

        String msg = JSONObject.toJSONString(orderParam);

        rabbitTemplate.convertAndSend(RabbitMQConfig.ORDER_EXCHANGE,RabbitMQConfig.ORDER_ROUTING_KEY,msg);

        rabbitTemplate.convertAndSend(RabbitMQConfig.TEST_EXCHANGE,RabbitMQConfig.TEST_ROUTING_KEY,msg);

        return ResponseData.success("发送消息成功！");
    }



}
